package com.laixu.estore.service;

import com.laixu.estore.bean.pojo.Shopcar;
import com.laixu.estore.bean.vo.ShopcarVo;
import com.laixu.estore.dao.ShopCarDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Auther: vanse(lc)
 * @Date: 2025/4/11-04-11-11:43
 * @Description：com.briup.estore.service
 */
@Service
@RequiredArgsConstructor
public class ShopCarService {
    private final ShopCarDao shopCarDao;
    public void addCar(int userId, Integer bookId) {
        // 根据用户id和书籍id去购物车查询是否有数据
        Shopcar shopcarDB =
                shopCarDao.findCarByuserIdAndBookId(userId,bookId);
        if(ObjectUtils.isEmpty(shopcarDB)){
            // 如果是第一次添加购物车]
            Shopcar shopcar = new Shopcar();
            shopcar.setUserId(userId);
            shopcar.setBookId(bookId);
            shopcar.setNum(1);
            shopCarDao.insertCar(shopcar);
        }else{
            // 如果不是第一次
            shopcarDB.setNum(shopcarDB.getNum()+1);
            shopCarDao.updateCar(shopcarDB);
        }
    }

    public List<ShopcarVo> getShopCar(String userId) {
        return shopCarDao.getShopCar(userId);
    }
}